package com.aal.model;

import java.util.Date;

/**
 *
 * @author Hoang
 */
public class Checkout {

    private Cart cart;

    /**
     * Set the value of cart
     *
     * @param cart new value of cart
     */
    public void setCart(Cart cart) {
        this.cart = cart;
    }
    
    private Shipping shipping;

    /**
     * Set the value of shipping
     *
     * @param shipping new value of shipping
     */
    public void setShipping(Shipping shipping) {
        this.shipping = shipping;
    }

    private String username;

    /**
     * Set the value of username
     *
     * @param username new value of username
     */
    public void setUsername(String username) {
        this.username = username;
    }


    public boolean getCreateOrder() {
        OrderManagement om = new OrderManagement();
        OrderStatus orderStatus = new OrderStatus();
        
        om.setUsername(username);
        om.setOrderdate(new Date());
        om.setTotalnetprice(cart.getNetAmount());
        om.setDiscountvalue(cart.getDiscountAmount());
        om.setShippingcost(cart.getShippingAmount());
        om.setTaxcost(cart.getTaxAmount());
        om.setTotalprice(cart.getTotalAmount());
        om.setStatus(orderStatus.getPending());
        om.setShippingaddress(shipping.getShippingAddress());
        om.setReceivername(shipping.getReceiverName());
        om.setReceiverphone(shipping.getReceiverPhone());
        om.setCustomername(shipping.getCustomerName());
        om.setBillingaddress(shipping.getBillingAddress());
        
        
        long orderID = om.getPlaceOrder();
        
        if (orderID == -1) return false;
        
        for (ProductInCart item : cart.getItems()) {
            boolean ok = om.createOrderDetail(orderID, item.getProduct().getProductID(), item.getQuantity(), 
                    item.getNetAmount(), item.getShippingAmount(), item.getTaxAmount());
            if (!ok) return false;
        }
        
        return true;
    }

}
